//
//  lxRootViewController.m
//  luodansc
//
//  Created by 倫 星 on 2012/10/29.
//  Copyright (c) 2012年 方正株式会社. All rights reserved.
//

#import "lxRootViewController.h"
#import "lxGoodsEntity.h"
#import "lxWeiBoEntity.h"
#import "lxGoodsDetailViewController.h"
#import "lxCategoryCell.h"
#import "lxCategoryInfoEntity.h"
#import "lxGoodsListViewController.h"


#ifndef LUN_SELECT_CATEGORY_LIST_COUNT
    #define LUN_SELECT_CATEGORY_LIST_COUNT 20
#endif

#ifndef LUN_VALUE_EMPTY
    #define LUN_VALUE_EMPTY -999
#endif

#ifndef LUN_SELECT_TOP_COUNT
    #define LUN_SELECT_TOP_COUNT 8
#endif

@interface lxRootViewController ()

@end

@implementation lxRootViewController

@synthesize pfv = _pfv;
@synthesize ppcv = _ppcv;
@synthesize horizMenu = _horizMenu;

- (id)init {
    self = [super init];
    if (nil != self) {
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"luodan_shop", nil) image:[UIImage imageNamed:@"tabbar-heart.png"] tag:0];
        self.tabBarItem = item;
        [item release];
        
        request_case = ROOT_REFLASH_ALL;
    }
    
    return self;
}

- (void)initPagedFlowView:(CGRect)frame {
    _pfv_arrayList = [[NSMutableArray alloc]initWithCapacity:5];
    
    self.pfv = [[PagedFlowView alloc] initWithFrame:frame];
    
    self.pfv.delegate = self;
    self.pfv.dataSource = self;
    self.pfv.minimumPageAlpha = 0.3;
    self.pfv.minimumPageScale = 0.9;
    
    _pfv_pageNum = LUN_VALUE_EMPTY;
    
    [self.view addSubview:_pfv];
}

- (void)initPullPsCollectionView:(CGRect)frame {
    request_case = ROOT_REFLASH_ALL;
    
    _ppcv_arrayList = [[NSMutableArray alloc]initWithCapacity:5];
    
    self.ppcv = [[PullPsCollectionView alloc] initWithFrame:frame];
    self.ppcv.collectionViewDelegate = self;
    self.ppcv.collectionViewDataSource = self;
    self.ppcv.pullDelegate=self;
    self.ppcv.backgroundColor = [UIColor clearColor];
    self.ppcv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    
    self.ppcv.numColsPortrait = 4;
    self.ppcv.numColsLandscape = 8;
    
    self.ppcv.pullArrowImage = [UIImage imageNamed:@"blackArrow"];
    self.ppcv.pullBackgroundColor = [UIColor grayColor];
    self.ppcv.pullTextColor = [UIColor blackColor];  
    [self.view addSubview:self.ppcv];
}

- (void)initHorizMenuView:(CGRect)frame {
    _horizMenu_index = 0;
    _items_horizMenu = [[NSArray alloc] initWithObjects:NSLocalizedString(@"best", nil), NSLocalizedString(@"new", nil), NSLocalizedString(@"hot", nil), nil];
    
    self.horizMenu = [[MKHorizMenu alloc] initWithFrame:frame];
    self.horizMenu.delegate = self;
    self.horizMenu.dataSource = self;
    self.horizMenu.itemSelectedDelegate = self;

    [self.horizMenu reloadData];
    
    [self.view addSubview:self.horizMenu];
}

- (void)setHorizMenuSelectedInit {
    _horizMenu_index = 0;
    [self.horizMenu setSelectedInit];
}

- (void)subViewDidLoad {
    self.is_supportShakeToEdit = YES;

    float horizMenu_height = self.view.bounds.size.height / 20 * 1;
    CGRect horizMenu_ret = CGRectMake(self.view.bounds.size.width / 5, 0, self.view.bounds.size.width / 5 * 3, horizMenu_height);
    [self initHorizMenuView:horizMenu_ret];
    
    float pfv_height = self.view.bounds.size.height / 5 * 2 - horizMenu_height;
    
    CGRect pfv_ret = CGRectMake(0, horizMenu_height, self.view.bounds.size.width, pfv_height);
    [self initPagedFlowView:pfv_ret];
    
    CGRect clv_ret = CGRectMake(0, pfv_height + horizMenu_height, self.view.bounds.size.width, self.view.bounds.size.height - pfv_height - horizMenu_height);
    [self initPullPsCollectionView:clv_ret];
    
    [_arrayMenuImageList addObject:@"story-place.png"];
    [_arrayMenuImageList addObject:@"story-reload.png"];
    [_arrayMenuImageList addObject:@"story-thought.png"];
    [_arrayMenuImageList addObject:@"story-mail.png"];
    [_arrayMenuImageList addObject:@"story-camera.png"];
    [_arrayMenuImageList addObject:@"story-home.png"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
    self.title = NSLocalizedString(@"rodin_mind_title", nil);
    UIBarButtonItem *reflash_bt = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"reflash_bt", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(leftReflashBtClick:)];
    self.reFlash_bt = reflash_bt;
    [reflash_bt release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [self.horizMenu release];
    [_items_horizMenu release];
    [self.pfv release];
    [_pfv_arrayList release];
    _pfv_pageNum = LUN_VALUE_EMPTY;
    [self.ppcv release];
    [_ppcv_arrayList release];
    if (nil != _ppcv_arrayList_tmp) {
        [_ppcv_arrayList_tmp release];
        _ppcv_arrayList_tmp = nil;
    }
    request_case = ROOT_REFLASH_ALL;
    self.horizMenu.is_recommend_goods_loading = NO;
    [super dealloc];
}

- (void)reFlash {
    if(!self.ppcv.pullTableIsRefreshing) {
        lxAPPContainer *appContainer = [lxAPPContainer sharedLxAPPContainer];
        if (YES == [appContainer isExistNet]) {
            self.ppcv.pullTableIsRefreshing = YES;
            [self sendRequestAll];
        }
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

- (void)leftReflashBtClick:(id)sender {
    [self reFlash];
}

#pragma mark -
#pragma mark ASIHTTPRequestDelegate Methods
- (ASIHTTPRequest *)sendRequestAll {
    request_case = ROOT_REFLASH_ALL;
    self.horizMenu.is_recommend_goods_loading = YES;
    
    lxAPPContainer *appContainer = [lxAPPContainer sharedLxAPPContainer];
    if (NO == [appContainer isExistNet]) {
        return nil;
    }
    
    int start_row = 0;
    int row_count = LUN_SELECT_CATEGORY_LIST_COUNT;
    
    int cat_start_row = 0;
    int cat_row_count = LUN_SELECT_TOP_COUNT;
    NSString *str_url = [NSString stringWithFormat:@"%@%@?start_row=%d&row_count=%d&horiz_index=%d&cat_start_row=%d&cat_row_count=%d", service_url, getRootInfoServlet, start_row, row_count, _horizMenu_index, cat_start_row, cat_row_count];
    NSURL *url = [NSURL URLWithString:str_url];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request startAsynchronous];
    
    self.is_loading = YES;
    
    return request;
}

- (void)getResponseAll:(ASIHTTPRequest *)request {
    if (nil != _ppcv_arrayList_tmp) {
        [_ppcv_arrayList_tmp release];
        _ppcv_arrayList_tmp = nil;
    }
    
    NSString *responseString = [request responseString];
    
    if (![responseString isKindOfClass:[NSString class]]) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    if ([lxStringUtility isNil:responseString]) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    NSData *reponsedata = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    
    if (nil == reponsedata) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    if (![reponsedata isKindOfClass:[NSData class]]) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    __autoreleasing NSError *error = nil;
    
    NSDictionary *dictionaryAll = [NSJSONSerialization JSONObjectWithData:reponsedata options:kNilOptions error:&error];
    
    if (nil == dictionaryAll) {
        NSLog(@"init Response failed because:%@", [error localizedDescription]);
        return;
    }
    
    if (![dictionaryAll isKindOfClass:[NSDictionary class]]) {
        NSLog(@"init Response failed because:%@", [error localizedDescription]);
        return;
    }
    
    int count = 0;
    
    NSArray *arrayList_tmp = [dictionaryAll objectForKey:@"goods_contents_list"];
    
    if (nil == arrayList_tmp) {
        NSLog(@"goods_contents_list Response failed");
        return;
    }
    
    if (![arrayList_tmp isKindOfClass:[NSArray class]]) {
        NSLog(@"init Response failed because:%@", [error localizedDescription]);
        return;
    }
    
    count = [arrayList_tmp count];
    if (count < 1) {
        NSLog(@"goods_contents_list Response failed because:reponsedata is nothing");
        return;
    }
    
    [_pfv_arrayList removeAllObjects];
    
    for (NSDictionary *dictionary in arrayList_tmp) {
        if (nil != dictionary) {
            if ([dictionary isKindOfClass:[NSDictionary class]]) {
                [_pfv_arrayList addObject:dictionary];
            }
        }
    }
    
    _ppcv_arrayList_tmp = [dictionaryAll objectForKey:@"category_contents_list"];
    if (nil == _ppcv_arrayList_tmp) {
        NSLog(@"category_contents_list Response failed");
        return;
    }
    
    if (![_ppcv_arrayList_tmp isKindOfClass:[NSArray class]]) {
        NSLog(@"category_contents_list Response failed because:reponsedata is nothing");
        _ppcv_arrayList_tmp = nil;
        return;
    }
    
    count = [_ppcv_arrayList_tmp count];
    if (count < 1) {
        NSLog(@"category_contents_list Response failed because:reponsedata is nothing");
        _ppcv_arrayList_tmp = nil;
        return;
    }
    
    return;
}

- (void)requestFinished:(ASIHTTPRequest *)request {
    switch (request_case) {
        case REFLASH_TABLE: {
            [self getResponse:request];
            [self pullPsCollectionViewRefreshTable];
        }
            break;
        case LOAD_MORE_TABLE: {
            [self getResponse:request];
            [self pullPsCollectionViewLoadMoreDataToTable];
        }
            break;
        case RECOMMEND_GOODS: {
            [self getResponseHorizMenu:request];
            [self pagedFlowViewLoadDataSource];
        }
            break;
        default: {
            [self getResponseAll:request];
            [self pagedFlowViewLoadDataSource];
            [self pullPsCollectionViewRefreshTable];
        }
            break;
    }
    self.ppcv.pullTableIsRefreshing = NO;
    self.ppcv.pullTableIsLoadingMore = NO;
    request_case = ROOT_REFLASH_ALL;
    self.is_loading = NO;
    self.horizMenu.is_recommend_goods_loading = NO;
}

- (void)requestFailed:(ASIHTTPRequest *)request {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"error", nil)
                                                    message:NSLocalizedString(@"network_conn_failed", nil)
                                                   delegate:self
                                          cancelButtonTitle:NSLocalizedString(@"ok", nil)
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    self.ppcv.pullTableIsRefreshing = NO;
    self.ppcv.pullTableIsLoadingMore = NO;
    request_case = ROOT_REFLASH_ALL;
    self.is_loading = NO;
    self.horizMenu.is_recommend_goods_loading = NO;
}

////////////////////////pagedFlowView start/////////////////
- (void)pagedFlowViewLoadDataSource {
    int count = [_pfv_arrayList count];
    NSLog(@"%d", count);
    if (0 != count) {
        _pfv_pageNum = 0;
        NSDictionary *dictionary = [_pfv_arrayList objectAtIndex:0];
        
        lxGoodsEntity *goodsEntity = [[lxGoodsEntity alloc] init];
        
        BOOL result = [goodsEntity setEntity:dictionary];
        if (YES == result) {
            [self.pfv.lxTPCV setTitleAndPrice:goodsEntity.name price:goodsEntity.shop_price];
        }
        [self.pfv scrollToPage:0];
    }
    [self.pfv reloadData];
}

////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark PagedFlowView Delegate
- (CGSize)sizeForPageInFlowView:(PagedFlowView *)flowView;{
    float img_width = flowView.bounds.size.height;
    return CGSizeMake(img_width, img_width);
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(PagedFlowView *)flowView {
    NSLog(@"Scrolled to page # %d", pageNumber);
    if (nil == _pfv_arrayList) {
        return;
    }
    
    int count = [_pfv_arrayList count];
    if (pageNumber < count) {
        NSDictionary *dictionary = [_pfv_arrayList objectAtIndex:pageNumber];
        
        lxGoodsEntity *goodsEntity = [[lxGoodsEntity alloc] init];
        
        BOOL result = [goodsEntity setEntity:dictionary];
        if (YES == result) {
            [self.pfv.lxTPCV setTitleAndPrice:goodsEntity.name price:goodsEntity.shop_price];
        }
        _pfv_pageNum = pageNumber;
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark PagedFlowView Datasource
//返回显示View的个数
- (NSInteger)numberOfPagesInFlowView:(PagedFlowView *)flowView{
    if (nil == _pfv_arrayList) {
        return 1;
    }
    int count = [_pfv_arrayList count];
    if (0 == count) {
        return 1;
    }
    return count;
}

//返回给某列使用的View
- (UIView *)flowView:(PagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    if (nil == _pfv_arrayList) {
        return nil;
    }
    
    lxImageView *imageView = (lxImageView *)[flowView dequeueReusableCell];
    if (nil == imageView) {
        imageView = [[[lxImageView alloc] init] autorelease];
        imageView.delegate = self;
        imageView.layer.cornerRadius = 6;
        imageView.layer.masksToBounds = YES;
    }
    
    int count = [_pfv_arrayList count];
    NSLog(@"count:%d", count);
    
    lxGoodsEntity *goodsEntity = nil;
    if (index <= count - 1) {
        NSDictionary *dictionary = [_pfv_arrayList objectAtIndex:index];
        
        goodsEntity = [[lxGoodsEntity alloc] init];
        
        BOOL result = [goodsEntity setEntity:dictionary];
        if (YES == result) {
            NSString *str_url = [NSString stringWithFormat:@"%@%@", web_root, goodsEntity.goods_img];
            [imageView loadImageFromURL:str_url];
        }
    }
    
    return imageView;
}

- (lxWeiBoEntity *)getCurrentInfo {
    NSDictionary *dictionary = [_pfv_arrayList objectAtIndex:_pfv_pageNum];
    lxWeiBoEntity *lxWBE = [[lxWeiBoEntity alloc] init];
    
    BOOL result = [lxWBE setEntity:dictionary];
    
    if (NO == result) {
        return nil;
    }
    
    return lxWBE;
}

#pragma mark -
#pragma mark ImageTouchDelegate
-(void)imageTouch:(NSSet *)touches withEvent:(UIEvent *)event whichView:(id)imageView {
    NSLog(@"Scrolled to page # %d", _pfv_pageNum);
    if (LUN_VALUE_EMPTY == _pfv_pageNum) {
        return;
    }
    
    if (nil == _pfv_arrayList) {
        return;
    }
    
    int count = [_pfv_arrayList count];
    if (_pfv_pageNum < count) {
        NSDictionary *dictionary = [_pfv_arrayList objectAtIndex:_pfv_pageNum];
        
        lxGoodsEntity *goodsEntity = [[lxGoodsEntity alloc] init];
        
        BOOL result = [goodsEntity setEntity:dictionary];
        if (YES == result) {
            lxGoodsDetailViewController *lxGDVC = [[lxGoodsDetailViewController alloc] init];
            lxGDVC.lxGE.goods_id = goodsEntity.goods_id;
            [self.navigationController pushViewController:lxGDVC animated:YES];
            [lxGDVC reFlash];
            [lxGDVC release];
        }
    }
}
////////////////////////pagedFlowView end/////////////////

////////////////////////pullPsCollectionView start///////////////
- (ASIHTTPRequest *)sendRequest {
    lxAPPContainer *appContainer = [lxAPPContainer sharedLxAPPContainer];
    if (NO == [appContainer isExistNet]) {
        return nil;
    }
    
    int start_row = 0;
    if (LOAD_MORE_TABLE == request_case) {
        start_row = [_ppcv_arrayList count];
    }
    
    int row_count = LUN_SELECT_CATEGORY_LIST_COUNT;
    NSString *str_url = [NSString stringWithFormat:@"%@%@?start_row=%d&row_count=%d", service_url, getCategoryListServlet, start_row, row_count];
    NSURL *url = [NSURL URLWithString:str_url];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request startAsynchronous];
    return request;
}

- (void)getResponse:(ASIHTTPRequest *)request {
    _ppcv_arrayList_tmp = nil;
    NSString *responseString = [request responseString];
    
    if (![responseString isKindOfClass:[NSString class]]) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    if ([lxStringUtility isNil:responseString]) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    NSData *reponsedata = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    
    if (nil == reponsedata) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    if (![reponsedata isKindOfClass:[NSData class]]) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    __autoreleasing NSError *error = nil;
    
    _ppcv_arrayList_tmp = [NSJSONSerialization JSONObjectWithData:reponsedata options:kNilOptions error:&error];
    if (nil == _ppcv_arrayList_tmp) {
        NSLog(@"Response failed because:%@", [error localizedDescription]);
        return;
    }
    
    if (![_ppcv_arrayList_tmp isKindOfClass:[NSArray class]]) {
        NSLog(@"Response failed because:reponsedata is nothing");
        _ppcv_arrayList_tmp = nil;
        return;
    }
    
    int count = [_ppcv_arrayList_tmp count];
    if (count < 1) {
        NSLog(@"Response failed because:reponsedata is nothing");
        _ppcv_arrayList_tmp = nil;
        return;
    }
    return;
}

- (void)pullPsCollectionViewRefreshTable
{
    if(nil == _ppcv_arrayList_tmp) {
        return;
    }
    
    [_ppcv_arrayList removeAllObjects];
    [_ppcv_arrayList addObjectsFromArray:_ppcv_arrayList_tmp];
    [_ppcv_arrayList_tmp retain];
    if (nil != _ppcv_arrayList_tmp) {
        [_ppcv_arrayList_tmp release];
        _ppcv_arrayList_tmp = nil;
    }
    [self.ppcv reloadData];
    self.ppcv.pullLastRefreshDate = [NSDate date];
    self.ppcv.pullTableIsRefreshing = NO;
}

- (void)pullPsCollectionViewLoadMoreDataToTable
{
    if(nil == _ppcv_arrayList_tmp) {
        return;
    }
    
    [_ppcv_arrayList addObjectsFromArray:_ppcv_arrayList_tmp];
    [_ppcv_arrayList_tmp retain];
    if (nil != _ppcv_arrayList_tmp) {
        [_ppcv_arrayList_tmp release];
        _ppcv_arrayList_tmp = nil;
    }
    [self.ppcv reloadData];
    self.ppcv.pullTableIsLoadingMore = NO;
}

#pragma mark - PullTableViewDelegate
- (void)pullPsCollectionViewDidTriggerRefresh:(PullPsCollectionView *)pullTableView
{
    request_case = REFLASH_TABLE;
    [self sendRequest];
}

- (void)pullPsCollectionViewDidTriggerLoadMore:(PullPsCollectionView *)pullTableView
{
    request_case = LOAD_MORE_TABLE;
    [self sendRequest];
}

- (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView viewAtIndex:(NSInteger)index {
    int count = [_ppcv_arrayList count];
    NSLog(@"[tableView]arrayList count:%d", count);
    
    if (count < 1) {
        return nil;
    }
    
    // You should probably subclass PSCollectionViewCell
    lxCategoryCell *cell = (lxCategoryCell *)[self.ppcv dequeueReusableView];
    if(cell == nil) {
        CGRect ret = CGRectMake(0, 0, collectionView.bounds.size.width / 4, collectionView.bounds.size.height / 2);
        cell = [[[lxCategoryCell alloc] initWithFrame:ret] autorelease];
    }
    
    lxCategoryInfoEntity *entity = nil;
    if (index <= count - 1) {
        NSDictionary *dictionary = [_ppcv_arrayList objectAtIndex:index];
        
        entity = [[lxCategoryInfoEntity alloc] init];
        
        BOOL result = [entity setEntity:dictionary];
        if (YES == result) {
            [cell refreshData:entity];
        }
    }
    return cell;
}

- (CGFloat)heightForViewAtIndex:(NSInteger)index {
    float cell_height = self.ppcv.bounds.size.height / 2;
    return cell_height;
}

- (void)collectionView:(PSCollectionView *)collectionView didSelectView:(PSCollectionViewCell *)view atIndex:(NSInteger)index {
    // Do something with the tap
    NSLog(@"index=%d", index);
    if (nil == _ppcv_arrayList) {
        NSLog(@"arrayList is nil");
        return;
    }
    
    int arrayList_count = [_ppcv_arrayList count];
    NSLog(@"arrayList_count=%d", arrayList_count);
    if (arrayList_count - 1 < index) {
        return;
    }
    
    //Add Cell event.
    
    NSDictionary *dictionary = [_ppcv_arrayList objectAtIndex:index];
    lxCategoryInfoEntity *entity = [[lxCategoryInfoEntity alloc] init];
    BOOL result = [entity setEntity:dictionary];
    if (YES == result) {
        lxGoodsListViewController *lxGLVC = [[lxGoodsListViewController alloc] init];
        lxGLVC.cat_id = entity.cat_id;
        [self.navigationController pushViewController:lxGLVC animated:YES];
        [lxGLVC reFlash];
        [lxGLVC release];
    }
}

- (NSInteger)numberOfViewsInCollectionView:(PSCollectionView *)collectionView {
    return [_ppcv_arrayList count];
}
////////////////////////pullPsCollectionView end/////////////////

////////////////////////horizMenu start/////////////////
#pragma mark -
#pragma mark HorizMenu Data Source
- (UIImage*) selectedItemImageForMenu:(MKHorizMenu*) tabMenu
{
    return [[UIImage imageNamed:@"ButtonSelected"] stretchableImageWithLeftCapWidth:16 topCapHeight:0];
}

- (UIColor*) backgroundColorForMenu:(MKHorizMenu *)tabView
{
//    return [UIColor colorWithPatternImage:[UIImage imageNamed:@"MenuBar"]];
    return [UIColor whiteColor];
}

- (int) numberOfItemsForMenu:(MKHorizMenu *)tabView
{
    return [_items_horizMenu count];
}

- (NSString*) horizMenu:(MKHorizMenu *)horizMenu titleForItemAtIndex:(NSUInteger)index
{
    return [_items_horizMenu objectAtIndex:index];
}

#pragma mark -
#pragma mark HorizMenu Delegate
-(void) horizMenu:(MKHorizMenu *)horizMenu itemSelectedAtIndex:(NSUInteger)index
{
    if (_horizMenu_index == index) {
        return;
    }
    
    if (nil == _items_horizMenu) {
        return;
    }
    
    int count = [_items_horizMenu count];
    NSLog(@"%d", count);
    if (count > index) {
        _horizMenu_index = index;
        [self sendRequestHorizMenu];
    }
}

- (ASIHTTPRequest *)sendRequestHorizMenu {
    request_case = RECOMMEND_GOODS;
    self.horizMenu.is_recommend_goods_loading = YES;
    
    lxAPPContainer *appContainer = [lxAPPContainer sharedLxAPPContainer];
    if (NO == [appContainer isExistNet]) {
        return nil;
    }
    
    int start_row = 0;
    int row_count = LUN_SELECT_CATEGORY_LIST_COUNT;
    
    NSString *str_url = [NSString stringWithFormat:@"%@%@?start_row=%d&row_count=%d&horiz_index=%d", service_url, getRecommendGoodsServlet, start_row, row_count, _horizMenu_index];
    NSURL *url = [NSURL URLWithString:str_url];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request startAsynchronous];
    
    return request;
}

- (void)getResponseHorizMenu:(ASIHTTPRequest *)request {
    NSString *responseString = [request responseString];
    
    if (![responseString isKindOfClass:[NSString class]]) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    if ([lxStringUtility isNil:responseString]) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    NSData *reponsedata = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    
    if (nil == reponsedata) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    if (![reponsedata isKindOfClass:[NSData class]]) {
        NSLog(@"reponsedate is nil");
        return;
    }
    
    __autoreleasing NSError *error = nil;
    
    NSArray *arrayList_tmp = [NSJSONSerialization JSONObjectWithData:reponsedata options:kNilOptions error:&error];
    
    if (nil == arrayList_tmp) {
        NSLog(@"init Response failed because:%@", [error localizedDescription]);
        return;
    }
    
    if (![arrayList_tmp isKindOfClass:[NSArray class]]) {
        NSLog(@"goods_contents_list Response failed because:reponsedata is nothing");
        return;
    }
    
    int count = [arrayList_tmp count];
    if (count < 1) {
        NSLog(@"goods_contents_list Response failed because:reponsedata is nothing");
        return;
    }
    
    [_pfv_arrayList removeAllObjects];
    
    for (NSDictionary *dictionary in arrayList_tmp) {
        if (nil != dictionary) {
            if ([dictionary isKindOfClass:[NSDictionary class]]) {
                [_pfv_arrayList addObject:dictionary];
            }
        }
    }
    return;
}

////////////////////////horizMenu end/////////////////

@end
